package com.example.cyk.service.impl;

import com.example.cyk.config.exception.ApplicationException;
import com.example.cyk.config.response.AppReturn;
import com.example.cyk.config.response.CodeMsg;
import com.example.cyk.mapper.ArticleMapper;
import com.example.cyk.mapper.BoardMapper;
import com.example.cyk.mapper.UserinfoMapper;
import com.example.cyk.model.Article;
import com.example.cyk.model.Board;
import com.example.cyk.model.Userinfo;
import com.example.cyk.model.enums.BoardEnum;
import com.example.cyk.model.vo.ArticleVO;
import com.example.cyk.service.IArticleService;
import javafx.application.Application;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<String> getAllBoard() {
        return articleMapper.selectAllBoard();
    }

    @Override
    public List<ArticleVO> getArticleList(String board, Integer curPage, Integer pageSize) {
        if(curPage == null || pageSize == null || board == null) {
            log.warn(CodeMsg.FAIL_NULL_BOARD_CURPAGE_PAGESIZE.toString());
            throw new ApplicationException(AppReturn.fail(CodeMsg.FAIL_NULL_BOARD_CURPAGE_PAGESIZE));
        }
        //2.非法校验
        if(curPage <= 0 || pageSize <= 0) {
            log.warn(CodeMsg.FAIL_ERROR_CURPAGE_PAGESIZE.toString());
            throw new ApplicationException(AppReturn.fail(CodeMsg.FAIL_ERROR_CURPAGE_PAGESIZE));
        }
        //3.处理分页
        int limit = pageSize;
        int offset = (curPage - 1) * pageSize;
        //4.获取文章列表并整理
        List<Article> articles = null;
        List<ArticleVO> articleVOList = new ArrayList<>();
        Long boardId = boardMapper.selectBoardByName(board).getId();
        if(board.equals(BoardEnum.LATEST.getName())) {
            //如果板块选中的是 "最新" ，就意味着要在所有文章上分页
            articles = articleMapper.selectArticleList(limit, offset);
            if(articles == null) {
                //说明此时还没有文章(也算是正常)
                throw new ApplicationException(AppReturn.success(CodeMsg.NORMAL_NULL_ARTICLE));
            }
            //获取作者信息并填入 VO
            for (Article article : articles) {
                Userinfo author = userinfoMapper.selectUserinfoById(article.getUserId());
                ArticleVO articleVO = new ArticleVO();
                //填入
                BeanUtils.copyProperties(article, articleVO);
                articleVO.setNickname(author.getNickname());
                articleVO.setPhotoUrl(author.getPhotoUrl());
                articleVOList.add(articleVO);
            }
        } else {
            //否则就是在当前板块上进行分页
            articles = articleMapper.selectArticleListByBoard(boardId, limit, offset);
            //获取作者信息并填入 VO
            for (Article article : articles) {
                //只有是当前板块 id 的才被填入
                if(article.getBoardId().equals(boardId)) {
                    Userinfo author = userinfoMapper.selectUserinfoById(article.getUserId());
                    ArticleVO articleVO = new ArticleVO();
                    //填入
                    BeanUtils.copyProperties(article, articleVO);
                    articleVO.setNickname(author.getNickname());
                    articleVO.setPhotoUrl(author.getPhotoUrl());
                    articleVOList.add(articleVO);
                }
            }
        }
        return articleVOList;
    }

    @Override
    public Integer getTotal(String board) {
        if(board == null) {
            log.warn(CodeMsg.FAIL_NULL_BOARD.toString());
            throw new ApplicationException(AppReturn.fail(CodeMsg.FAIL_NULL_BOARD));
        }
        Long boardId = boardMapper.selectBoardByName(board).getId();
        Integer total = null;
        if(board.equals(BoardEnum.LATEST.getName())) {
            //1.如果是最新，就获取全部
            total = articleMapper.getTotal();
        } else {
            //2.否则获取对应板块
            total = articleMapper.getTotal(boardId);
        }
        return total;
    }

    @Override
    public void addArticle(ArticleVO articleVO) {
        //1.非空校验
        if(articleVO == null || !StringUtils.hasLength(articleVO.getTitle()) ||
                !StringUtils.hasLength(articleVO.getDesc()) ||
                !StringUtils.hasLength(articleVO.getContent()) ||
                !StringUtils.hasLength(articleVO.getBoard())) {
            log.warn(CodeMsg.FAIL_NULL_ARTICLE.toString());
            throw new ApplicationException(AppReturn.fail(CodeMsg.FAIL_NULL_ARTICLE));
        }
        //2.准备文章数据
        Article article = getArticleByVO(articleVO);
        //3.添加文章
        articleMapper.add(article);
    }

    /**
     * 准备文章数据
     * @param articleVO
     */
    private Article getArticleByVO(ArticleVO articleVO) {
        Article article = new Article();
        article.setUserId(articleVO.getUserId());
        article.setTitle(articleVO.getTitle());
        article.setDesc(articleVO.getDesc());
        article.setContent(articleVO.getContent());

        Long boardId = boardMapper.selectBoardByName(articleVO.getBoard()).getId();
        article.setBoardId(boardId);
        return article;
    }

}
